var Script = function () {

    $.validator.setDefaults({
        submitHandler: function(form) {
            var $form=$(form),$btn=$('button.js-ajax-submit');
            $form.ajaxSubmit({
                url: $btn.data('action') ? $btn.data('action') : $form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
                dataType: 'json',
                beforeSubmit: function (arr, $form, options) {

                    $btn.data("loading",true);
                    var text = $btn.text();

                    //按钮文案、状态修改
                    $btn.text(text + '中...').prop('disabled', true).addClass('disabled');
                },
                success: function (data, statusText, xhr, $form) {
                    var text = $btn.text();

                    //按钮文案、状态修改
                    $btn.removeClass('disabled').prop('disabled', false).text(text.replace('中...', '')).parent().find('span').remove();
                    if (data.state === 'success') {
                        $('<span class="tips_success">' + data.info + '</span>').appendTo($btn.parent()).fadeIn('slow').delay(1000).fadeOut(function () {
                        });
                    } else if (data.state === 'fail') {
                        var $verify_img=$form.find(".verify_img");
                        if($verify_img.length){
                            $verify_img.attr("src",$verify_img.attr("src")+"&refresh="+Math.random());
                        }

                        var $verify_input=$form.find("[name='verify']");
                        $verify_input.val("");

                        $('<span class="tips_error">' + data.info + '</span>').appendTo($btn.parent()).fadeIn('fast');
                        $btn.removeProp('disabled').removeClass('disabled');
                    }
                    if (data.referer) {
                        /*提示信息，然后1秒之后跳转*/
                        //返回带跳转地址 status
                        setInterval(function(){
                            window.location.href = data.referer;
                        },1000);
                    } else {
                        if (data.state === 'success') {
                            //刷新当前页
                            reloadPage(window);
                        }
                    }

                },
                error:function(xhr,e,statusText){
                    art.dialog({
                        id: 'warning',
                        icon: 'warning',
                        content: statusText,
                        cancelVal: '关闭',
                        cancel: function () {
                            reloadPage(window);
                        },
                        ok: function () {
                            reloadPage(window);
                        }
                    });

                },
                complete: function(){
                    $btn.data("loading",false);
                }
            });
        }
    });

    $().ready(function() {
        // validate the comment form when it is submitted
        $("#commentForm").validate();

        // validate signup form on keyup and submit
        $("#signupForm").validate({
            rules: {
                firstname: "required",
                lastname: "required",
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                },
                email: {
                    required: true,
                    email: true
                },
                topic: {
                    required: "#newsletter:checked",
                    minlength: 2
                },
                agree: "required"
            },
            messages: {
                firstname: "Please enter your firstname",
                lastname: "Please enter your lastname",
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                },
                email: "Please enter a valid email address",
                agree: "Please accept our policy"
            }
        });

        // propose username by combining first- and lastname
        $("#username").focus(function() {
            var firstname = $("#firstname").val();
            var lastname = $("#lastname").val();
            if(firstname && lastname && !this.value) {
                this.value = firstname + "." + lastname;
            }
        });

        //code to hide topic selection, disable for demo
        var newsletter = $("#newsletter");
        // newsletter topics are optional, hide at first
        var inital = newsletter.is(":checked");
        var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
        var topicInputs = topics.find("input").attr("disabled", !inital);
        // show when newsletter is checked
        newsletter.click(function() {
            topics[this.checked ? "removeClass" : "addClass"]("gray");
            topicInputs.attr("disabled", !this.checked);
        });
    });


}();